﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Xml;

using Wiki.Web.Models;

namespace Wiki.Web.Views.Page
{
    public partial class AtomRepresentation : ViewPage<WikiPage>
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            Response.ContentType = "application/atom+xml";
            Response.AddHeader("Content-Language", "en");
            Response.AddHeader("Content-Location", Request.Url.ToString(".atom", String.Empty));

            if (this.ViewData.IsAvailable)
            {
                Response.Cache.SetCacheability(HttpCacheability.Public);
                Response.Cache.SetLastModified(this.ViewData.LastModified.Value);
            }
            else
            {
                Response.StatusCode = 404; // Not Found
            }

            this.RenderContent();
        }

        private void RenderContent()
        {
            using (XmlTextWriter xml = new XmlTextWriter(Response.OutputStream, Encoding.UTF8))
            {
                xml.WriteStartDocument(true);
                #region <feed>
                xml.WriteStartElement("feed");
                xml.WriteAttributeString("xml", "lang", null, "en");
                xml.WriteAttributeString("xmlns", null, "http://www.w3.org/2005/Atom");
                #endregion
                // Required elements
                #region <id>_:…</id>
                if (this.ViewData.IsAvailable)
                {
                    xml.WriteElementString("id", "_:" + this.ViewData.Guid);
                }
                else
                {
                    xml.WriteElementString("id", "_:" + Guid.Empty);
                }
                #endregion
                #region <title type="text">…</title>
                xml.WriteStartElement("title");
                xml.WriteAttributeString("type", "text");
                xml.WriteString(String.Format(CultureInfo.InvariantCulture, "\"{0}\" Atom version feed", this.ViewData.Title));
                xml.WriteEndElement();
                #endregion
                #region <updated>…</updated>
                if (this.ViewData.IsAvailable)
                {
                    xml.WriteElementString("updated", XmlConvert.ToString(this.ViewData.LastModified.Value, XmlDateTimeSerializationMode.Utc));
                }
                else
                {
                    xml.WriteElementString("updated", XmlConvert.ToString(DateTime.UtcNow, XmlDateTimeSerializationMode.Utc));
                }
                #endregion
                // Recommended elements
                #region <author>…</author>
                xml.WriteStartElement("author");
                #region <name>…</name>
                xml.WriteElementString("name", "Author Name here");
                #endregion
                #region <email>…</email>
                xml.WriteElementString("email", "author@example.net");
                #endregion
                #region <uri>…</uri>
                xml.WriteElementString("uri", "http://example.net/page/user:author");
                #endregion
                xml.WriteEndElement();
                #endregion
                #region <link href="…" rel="source" title="…"/>
                xml.WriteStartElement("link");
                xml.WriteAttributeString("href", Request.Url.ToString(".atom", String.Empty));
                xml.WriteAttributeString("rel", "source");
                xml.WriteAttributeString("title", String.Format(CultureInfo.InvariantCulture, "\"{0}\" resource", this.ViewData.Title));
                xml.WriteEndElement();
                #endregion
                #region <link href="…" rel="self" type="…" hreflang="…" title="…"/>
                xml.WriteStartElement("link");
                xml.WriteAttributeString("href", Request.Url.OriginalString);
                xml.WriteAttributeString("rel", "self");
                xml.WriteAttributeString("type", "application/atom+xml");
                xml.WriteAttributeString("hreflang", "en");
                xml.WriteAttributeString("title", String.Format(CultureInfo.InvariantCulture, "\"{0}\" Atom version feed", this.ViewData.Title));
                xml.WriteEndElement();
                #endregion
                #region <link href="…" rel="alternate" type="…" hreflang="…" title="…"/> (HTML)
                xml.WriteStartElement("link");
                xml.WriteAttributeString("href", Request.Url.ToString(".atom", ".html"));
                xml.WriteAttributeString("rel", "alternate");
                xml.WriteAttributeString("type", "text/html");
                xml.WriteAttributeString("hreflang", "en");
                xml.WriteAttributeString("title", String.Format(CultureInfo.InvariantCulture, "\"{0}\" page in HTML", this.ViewData.Title));
                xml.WriteEndElement();
                #endregion
                #region <link href="…" rel="alternate" type="…" hreflang="…" title="…"/> (text)
                xml.WriteStartElement("link");
                xml.WriteAttributeString("href", Request.Url.ToString(".atom", ".txt"));
                xml.WriteAttributeString("rel", "alternate");
                xml.WriteAttributeString("type", "text/plain");
                xml.WriteAttributeString("hreflang", "en");
                xml.WriteAttributeString("title", String.Format(CultureInfo.InvariantCulture, "\"{0}\" page in plain text", this.ViewData.Title));
                xml.WriteEndElement();
                #endregion
                // Optional elements
                #region <category term="…"/>
                xml.WriteStartElement("category");
                xml.WriteAttributeString("term", "article");
                xml.WriteEndElement();
                #endregion
                #region <contributor>…</contributor>
                xml.WriteStartElement("contributor");
                #region <name>…</name>
                xml.WriteElementString("name", "Contributor Name here");
                #endregion
                #region <email>…</email>
                xml.WriteElementString("email", "contributor@example.net");
                #endregion
                #region <uri>…</uri>
                xml.WriteElementString("uri", "http://example.net/page/user:contributor");
                #endregion
                xml.WriteEndElement();
                #endregion
                #region <generator uri="…" version="…">…</generator>
                xml.WriteStartElement("generator");
                xml.WriteAttributeString("uri", "http://example.net/wiki");
                xml.WriteAttributeString("version", "1.0");
                xml.WriteString("Example Wiki");
                xml.WriteEndElement();
                #endregion
                #region <icon>…</icon>
                xml.WriteElementString("icon", "/Content/icon.jpg");
                #endregion
                #region <logo>…</logo>
                xml.WriteElementString("logo", "/Content/logo.jpg");
                #endregion
                #region <rights type="text">…</rights>
                xml.WriteStartElement("rights");
                xml.WriteAttributeString("type", "text");
                xml.WriteString(String.Format(CultureInfo.InvariantCulture, "Copyright © 2007-{0} Example Wiki", DateTime.UtcNow.Year));
                xml.WriteEndElement();
                #endregion
                #region <subtitle type="text">…</subtitle>
                xml.WriteStartElement("subtitle");
                xml.WriteAttributeString("type", "text");
                xml.WriteString(String.Format(CultureInfo.InvariantCulture, "You can track changes to the \"{0}\" wiki page by subscribing to this feed.", this.ViewData.Title));
                xml.WriteEndElement();
                #endregion

                if (this.ViewData.IsAvailable)
                {
                    #region <entry>…</entry>
                    xml.WriteStartElement("entry");
                    // Required elements
                    #region <id>_:…</id>
                    xml.WriteElementString("id", String.Format(CultureInfo.InvariantCulture, "_:{0}/{1}", this.ViewData.Guid, XmlConvert.ToString(this.ViewData.LastModified.Value, XmlDateTimeSerializationMode.Utc)));
                    #endregion
                    #region <title type="text">…</title>
                    xml.WriteStartElement("title");
                    xml.WriteAttributeString("type", "text");
                    xml.WriteString(this.ViewData.Title);
                    xml.WriteEndElement();
                    #endregion
                    #region <updated>…</updated>
                    xml.WriteElementString("updated", XmlConvert.ToString(this.ViewData.LastModified.Value, XmlDateTimeSerializationMode.Utc));
                    #endregion
                    // Recommended elements
                    #region <link href="…" rel="source" title="…"/>
                    xml.WriteStartElement("link");
                    xml.WriteAttributeString("href", Request.Url.ToString(".atom", String.Empty));
                    xml.WriteAttributeString("rel", "source");
                    xml.WriteAttributeString("title", String.Format(CultureInfo.InvariantCulture, "\"{0}\" resource", this.ViewData.Title));
                    xml.WriteEndElement();
                    #endregion
                    #region <link href="…" rel="alternate" type="…" hreflang="…" title="…"/> (HTML)
                    xml.WriteStartElement("link");
                    xml.WriteAttributeString("href", Request.Url.ToString(".atom", ".html"));
                    xml.WriteAttributeString("rel", "alternate");
                    xml.WriteAttributeString("type", "text/html");
                    xml.WriteAttributeString("hreflang", "en");
                    xml.WriteAttributeString("title", String.Format(CultureInfo.InvariantCulture, "\"{0}\" page in HTML", this.ViewData.Title));
                    xml.WriteEndElement();
                    #endregion
                    #region <content type="xhtml">…</content>
                    xml.WriteStartElement("content");
                    xml.WriteAttributeString("type", "xhtml");
                    xml.WriteRaw(String.Format(CultureInfo.InvariantCulture, "<div xmlns=\"http://www.w3.org/1999/xhtml\">{0}</div>", this.ViewData.BodyAsHtml));
                    xml.WriteEndElement();
                    #endregion
                    xml.WriteEndElement();
                    #endregion
                }

                #region </feed>
                xml.WriteEndElement();
                #endregion
                xml.WriteEndDocument();
            }
        }
    }
}